#version 150
#extension GL_EXT_gpu_shader4 : enable

out vec4 oColor;

uniform sampler2DMS depthTex;
uniform vec4 lightColor;
uniform samplerBuffer sampleWeight;

//smooth in vec4 oPos;
smooth in vec4 texPos;

void main(void)
{
	vec4 newTexPos = texPos / texPos.w;
	
	vec2 iTmp = textureSize(depthTex);
	vec2 tmp = floor(iTmp * newTexPos.xy);
	
	float weight = 0;
	//float depth = texture(depthTex, newTexPos.xy).r;
	for(int i = 0; i < 32; ++i)
	{
		float sampleDepth = texelFetch(depthTex, ivec2(tmp), i).r;
		if(newTexPos.z > sampleDepth)
			weight += texelFetchBuffer(sampleWeight, i).r;
			//weight += 1.0/32.0;
			
	}
	
	if(newTexPos.x < 0.0f || newTexPos.x > 1.0f || 
	   newTexPos.y < 0.0f || newTexPos.y > 1.0f ||
	   newTexPos.z < 0.0f || newTexPos.z > 1.0f)
	   oColor = vec4(0.1, 0.1, 0.1, 1.0);
	//else if(newTexPos.z > depth)
		//oColor = vec4(0.1, 0.1, 0.1, 1.0);
	else
		oColor = lightColor * (1.0 - weight);
		
	oColor.a = 1.0;
	//oColor = vec4(0, 1.0, 1.0, 1.0);
}
